package com.firefly.util.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/*
 * 一些常用的方法，放到这里来
 */
public class FuncUtils {
    // 浮点数减法
    public static Float floatSubtract(Float a,Float b){
        BigDecimal aB = new BigDecimal(a);
        BigDecimal bB = new BigDecimal(b);
        return aB.subtract(bB).setScale(2, RoundingMode.HALF_UP).floatValue();
    }
    // 浮点数乘法
    public static Float floatMult(Float a,Float b){
        BigDecimal aB = new BigDecimal(a);
        BigDecimal bB = new BigDecimal(b);
        return aB.multiply(bB).setScale(2, RoundingMode.HALF_UP).floatValue();
    }
    public static BigDecimal floatMult(String[] f){
        BigDecimal result = new BigDecimal(1);
        for (String s:f) {
            result = result.multiply(new BigDecimal(s));
        }
        return result.setScale(2, RoundingMode.HALF_UP);
    }
    public static Double doubleMult(Double a,Double b){
        BigDecimal aB = new BigDecimal(a);
        BigDecimal bB = new BigDecimal(b);
        return aB.multiply(bB).doubleValue();
    }
    // 生成密码和密钥
    public static Map getPassword(String pwd,int length){
        String crypt = getRandomString(length);
        pwd = CryptUtils.encryptToMD5(CryptUtils.encryptToMD5(pwd + "." + crypt + "." + pwd));
        Map result = new HashMap<>();
        result.put("crypt",crypt);
        result.put("pwd",pwd);
        return result;
    }

    /*
     * length用户要求产生字符串的长度
     */
    public static String getRandomNumber(final int length) {
        final String str = "0123456789";
        final Random random = new Random();
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            final int number = random.nextInt(10);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /*
     * length用户要求产生字符串的长度
     */
    public static String getRandomString(final int length) {
        final String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        final Random random = new Random();
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            final int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 以UUID的策略生成一个长度为32的字符串，在同一时空中具有唯一性。
     *
     * @return UUID128位长度字符串
     */
    public static String getUUIDString() {
        String id = UUID.randomUUID().toString();
        id = id.replace("-", "");
        return id;
    }

}
